package com.zheng.study.base.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具类 - 公用
 * ============================================================================
 * author : fallenpanda
 * createDate:  2018/6/11 。
 * ============================================================================
 */
public class CommonUtil {

	/**
	 * 正则匹配
	 * @param regex 正则表达式
	 * @param str 需要验证的字符串
	 * @return boolean
	 */
	private static  boolean match(String regex, String str){
		boolean bool;
		if(StringUtils.isEmpty(regex)){
			bool = false;
		}else{
			if(StringUtils.isEmpty(str)){
				bool = false;
			}else{
				bool = Pattern.compile(regex).matcher(str).matches();
			}
		}
		return bool;
	}


	/**
	 * 用户名验证
	 * @param username 用户名
	 * @return boolean
	 */
	public static boolean isUsername(String username){
		String regex_username = "^[a-zA-Z]\\w{5,19}$";
		return match(regex_username, username);
	}

	/**
	 * 手机号验证
	 * @param mobile 手机号
	 * @return boolean
	 */
	public static boolean isMobile(String mobile) {
		String regex_mobile = "^[1][0,1,2,3,4,5,6,7,8,9][0-9]{9}$";
		return match(regex_mobile, mobile);
	}

	/**
	 * 邮箱验证
	 * @param email 邮箱
	 * @return boolean
	 */
	public static boolean isEmail(String email)   {
		String regex_email = "[a-z0-9A-Z_.-]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
		return match(regex_email, email);
	}

	/**
	 * 日期验证
	 * @param date 日期
	 * @return boolean
	 */
	public static boolean isDate(String date) {
		String regex_date = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		return match(regex_date, date);
	}

	/**
	 * 判断数字是否是整数 负数 小数
	 * @param number 数字字符串
	 * @return boolean
	 */
	public static boolean isNumber(String number){
		String regex_number = "^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$";
		return match(regex_number, number);
	}

	/**
	 * 有效查询属性值 字符 数字 下划线 点（防止sql注入）
	 * @param field 。。
	 * @return boolean
	 */
	public static boolean isField(String field){
		String regex_field = "[a-zA-Z0-9_、.`\"]*";
		return match(regex_field, field);
	}

	/**
	 * 根据制定长度 分隔字符串
	 * @param str 需要处理的字符串
	 * @param length 分隔长度
	 * @return 字符串集合
	 */
	public static List<String> splitString(String str, int length){
		List<String> list = new ArrayList<String>();
		for(int i=0; i<str.length(); i++){
			int endIndex = i+length;
			if(endIndex <= str.length()){
				list.add(str.substring(i, i+length));
			}else{
				list.add(str.substring(i, str.length()-1));
			}
		}
		return list;
	}

	/**
	 * 将字符串List转化为字符串，以分隔符间隔.
	 * @param strings 字符串集合
	 * @param separator 分隔符
	 * @return 转化后的字符串
	 */
	public static String toString(Collection<String> strings, String separator){
		return toString(strings, separator, false);
	}

	/**
	 * 将字符串List转化为字符串，以分隔符间隔.
	 * @param strings 字符串集合
	 * @param separator 分隔符
	 * @return 转化后的字符串
	 */
	public static String toString(Collection<String> strings, String separator, boolean around){
		StringBuffer sb = new StringBuffer();
		for(String str:strings){
			sb.append(separator);
			if(around) sb.append("");
			sb.append(str);
			if(around) sb.append("");
		}
		sb.deleteCharAt(0);
		return sb.toString();
	}

	/**
	 * 字符串数组转化字符串，以分隔符间隔.
	 * @param strings 字符串数组
	 * @param separator 分隔符
	 * @return String
	 */
	public static String toString(String[] strings, String separator) {
		return toString(strings, separator, false);
	}

	/**
	 * 字符串数组转化字符串
	 * @param strings 字符串数组
	 * @param separator 分隔符
	 * @param around 单引号
	 * @return String
	 */
	public static String toString(String[] strings, String separator, boolean around) {
		StringBuffer stringBuffer = new StringBuffer();
		for (String str : strings) {
			stringBuffer.append(separator);
			if (around) stringBuffer.append("'");
			stringBuffer.append(str);
			if (around) stringBuffer.append("'");
		}
		stringBuffer.deleteCharAt(0);
		return stringBuffer.toString();
	}

	public static String getAliasPath(String[] str, int index, int begin){
		String result = "";
		if(index < 0){
			return result;
		}
		if(index > str.length){
			index = str.length-1;
		}
		for(int i=begin;i<=index;i++){
			if(i==begin){
				result = str[i];
			}else{
				result = result + "." + str[i];
			}
		}
		return result;
	}

	public static String getAliasPath(String[] str, int index){
		return getAliasPath(str, index, 0);
	}

	public static String getPath(String[] str, int index, int begin){
		String result = "";
		if(index < 0){
			return result;
		}
		if(index > str.length){
			index = str.length-1;
		}
		for(int i=begin;i<=index;i++){
			if(i==begin){
				result = str[i];
			}else{
				if(i==index){
					result = result + "." + str[i];
				}else{
					result = result + "_" + str[i];
				}
			}
		}
		return result;
	}
	public static String getPath(String[] str, int index){
		return getPath(str, index, 0);
	}

	public static String getAlias(String[] str, int index, int begin){
		String result = "";
		if(index < 0){
			return result;
		}
		if(index > str.length){
			index = str.length-1;
		}
		for(int i=begin;i<=index;i++){
			if(i==begin){
				result = str[i];
			}else{
				result = result + "_" + str[i];
			}
		}
		return result;
	}
	public static String getAlias(String[] str, int index){
		return getAlias(str, index, 0);
	}

	public static String getRandomString(int length) {
		if(length <= 0) {
			return "";
		} else {
			char[] randomChar = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
			Random random = new Random();
			StringBuffer stringBuffer = new StringBuffer();

			for(int i = 0; i < length; ++i) {
				stringBuffer.append(randomChar[Math.abs(random.nextInt()) % randomChar.length]);
			}

			return stringBuffer.toString();
		}
	}

	/**
	 * 获取请求路径
	 */
	public static String getServerPath(HttpServletRequest request) {
		return request == null?null:request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
	}

}
